<template>
  <div class="app-container dict-bank-list-wrap">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="银行code" prop="bankCode">
        <el-input v-model="queryParams.bankCode"></el-input>
      </el-form-item>
      <el-form-item label="银行名称" prop="bankName">
        <el-input v-model="queryParams.bankName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="clickAddbtn" v-hasPermi="['config:bank:add']">
          添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="handleDelete"
          v-hasPermi="['config:bank:del']">删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" max-height="550" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handelEdit(scope.row)" v-hasPermi="['config:bank:edit']">编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="银行code" prop="bankCode" />
      <el-table-column label="银行名称" prop="bankName"></el-table-column>
      <el-table-column label="创建日期" prop="createTime" width="160px" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="ruleForm.id?'编辑银行卡':'添加银行卡'" :visible.sync="dialogVisible" width="600px">
      <div class="dialog-form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">

          <el-form-item label="银行code：" prop="bankCode">
            <el-input v-model="ruleForm.bankCode" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="银行名称：" prop="bankName">
            <el-input v-model="ruleForm.bankName" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="选择文件：" prop="bankImage">
            <el-upload class="upload-demo" :action="uploadUrl" accept="image/*" :headers="{'Authorization':token}"
              :on-progress="handelProgress" :on-success="uploadSuccess" :on-remove="handleRemove" :file-list="fileList"
              list-type="picture" style="width: 85%;">
              <el-button size="small" type="primary">点击上传</el-button>
              <span style="padding-left:15px">只能上传图片</span>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    bankList,
    bankAdd,
    bankEdit,
    bankDelBatch
  } from "@/api/system/dict/bank"
  import {
    getToken
  } from '@/utils/auth'
  export default {
    name: "DictBank",
    data() {
      return {
        uploadUrl: process.env.VUE_APP_BASE_API + '/system/oss/upload',
        loading: false,
        multiple: true,
        queryParams: {
          pageNum: 1,
          pageSize: 30
        },
        dialogVisible: false,
        ruleForm: {
          type: []
        },
        rules: {
          bankCode: [{
            required: true,
            message: "请输入银行Code",
            trigger: "change"
          }],
          bankName: [{
            required: true,
            message: "请输入银行名称",
            trigger: "change"
          }],
        },
        companyData: [],
        dataList: [],
        total: 0,
        fileList: [],
        seletIds: [],
        token: ''

      };
    },


    created() {
      this.getList()
      this.token = 'Bearer ' + getToken()
      console.log(this.token)
    },
    methods: {
      getList() {
        this.loading = true;
        bankList(this.queryParams).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false;
        })
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.seletIds = selection.map(item => item.id)
        this.bankIDArr = selection.map(item => item.bankCode)
        this.multiple = !selection.length
      },
      //批量删除产品
      handleDelete() {
        let that = this
        let ids = ''
        this.seletIds.forEach((item, i) => {
          if (this.seletIds.length - 1 == i) {
            ids += item
          } else {
            ids += item + ','
          }
        });
        this.$modal.confirm('是否确认删除客户ID为"' + this.bankIDArr + '"的数据项？').then(function () {
          bankDelBatch({
            ids: ids
          }).then(res => {
            that.getList();
            that.$modal.msgSuccess("删除成功");
          });
        }).catch(function () {});
      },

      handelProgress(event, file, fileList) {
        this.loadings = this.$loading({
          lock: true,
          text: '上传中，请稍等...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      },
      uploadSuccess(response, file, fileList) {
        console.log('response', response)
        let bankImage = ''
        if (this.loadings) this.loadings.close()
        if (!response.data) {
          this.$message.error('上传失败')
        } else {
          bankImage = response.data.url
        }
        this.fileList = [{
          url: bankImage
        }]
      },
      handleRemove(file, fileList) {
        this.fileList = []
        console.log(file, fileList);
      },
      // 表单重置
      reset() {
        this.resetForm("ruleForm");
        this.ruleForm = {
          id: '',
          bankCode: '',
          bankName: ''
        };
        this.fileList = []
      },
      //点击添加按钮弹出表单
      clickAddbtn() {
        this.dialogVisible = true
        this.reset()

      },
      //点击编辑弹出编辑表单并赋值
      handelEdit(row) {
        this.dialogVisible = true
        this.reset()
        this.ruleForm.id = row.id
        this.ruleForm.bankCode = row.bankCode,
          this.ruleForm.bankName = row.bankName
        this.fileList = row.bankImage ? [{
          url: row.bankImage
        }] : []
      },
      //点击提交表单
      handelConfirm() {
        console.log(this.ruleForm)
        this.$refs["ruleForm"].validate(valid => {
          if (valid) {
            const params = {
              id: this.ruleForm.id || null,
              bankCode: this.ruleForm.bankCode,
              bankName: this.ruleForm.bankName,
              bankImage: (this.fileList && this.fileList.length > 0) ? this.fileList[0].url : ''
            }
            if (this.ruleForm.id) {
              bankEdit(params).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.dialogVisible = false;
                this.getList();
              })
            } else {
              bankAdd(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            }
          }
        })
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">

</style>
